package com.chixing.day11.day11_Hom;

public class Multiply {
    public static String multiply(String num1, String num2) {
        if (num1.equals('0') || num2.equals('0') ){  //判断两个值,其中一个是否为零
            return "0";
        }

        int length1 = num1.length();
        int length2 = num2.length();
        int[] result = new int[length1 + length2];

        //从个位开始逐步相乘
        for (int i =  length1 - 1;i >= 0;i--){
            for (int j =  length2 - 1;j >= 0;j--){
                int killZero = (num1.charAt(i) - '0' ) * (num2.charAt(j) - '0');
                int index1 = i + j;        //
                int index2 = i + j + 1;    //乘积对应的下标
                int sum = killZero + result[index2]; //把余数和进位叠加到res数组上
                 result[index2] = sum % 10;
                 result[index1] += sum / 10;
            }
        }
        //确认结果前缀中是否有0
        int i = 0;
        while(i < result.length && result[i] == 0){
            i++;
        }

        StringBuffer str = new StringBuffer();
        for (; i < result.length; i++){
            str.append("" + result[i]);
        }
        return str.toString();
    }
    public static void main(String[] args) {
        String num1 = "555";
        String num2 = "456";
        System.out.println(multiply(num1,num2));
    }
}
